package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.CardExceptionRecord;
import com.amsp.oa.oademo.mapper.CardExceptionRecordMapper;
import com.amsp.oa.oademo.service.CardExceptionRecordService;
import com.amsp.oa.oademo.vo.CardExceptionRecordVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CardExceptionRecordServiceImpl extends ServiceImpl<CardExceptionRecordMapper, CardExceptionRecord> implements CardExceptionRecordService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdateCardExceptionRecord(CardExceptionRecordVO vo) {
        CardExceptionRecord entity = new CardExceptionRecord();
        BeanUtils.copyProperties(vo, entity);
        return this.saveOrUpdate(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchSaveOrUpdateCardExceptionRecord(List<CardExceptionRecordVO> voList) {
        List<CardExceptionRecord> entityList = voList.stream().map(vo -> {
            CardExceptionRecord entity = new CardExceptionRecord();
            BeanUtils.copyProperties(vo, entity);
            return entity;
        }).collect(Collectors.toList());
        return this.saveOrUpdateBatch(entityList);
    }

    @Override
    public CardExceptionRecordVO getCardExceptionRecordDetail(Long id) {
        CardExceptionRecord entity = this.getById(id);
        if (entity == null) {
            return null;
        }
        CardExceptionRecordVO vo = new CardExceptionRecordVO();
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }

    @Override
    public Page<CardExceptionRecordVO> getCardExceptionRecordPage(Page<CardExceptionRecord> page, CardExceptionRecordVO vo) {
        LambdaQueryWrapper<CardExceptionRecord> wrapper = new LambdaQueryWrapper<>();
        // 添加查询条件
        if (StringUtils.hasText(vo.getName())) {
            wrapper.like(CardExceptionRecord::getName, vo.getName());
        }
        if (StringUtils.hasText(vo.getEmployeeId())) {
            wrapper.like(CardExceptionRecord::getEmployeeId, vo.getEmployeeId());
        }
        if (StringUtils.hasText(vo.getDepartment())) {
            wrapper.like(CardExceptionRecord::getDepartment, vo.getDepartment());
        }
        if (vo.getRecordDate() != null) {
            wrapper.eq(CardExceptionRecord::getRecordDate, vo.getRecordDate());
        }

        Page<CardExceptionRecord> entityPage = this.page(page, wrapper);
        Page<CardExceptionRecordVO> voPage = new Page<>();
        BeanUtils.copyProperties(entityPage, voPage, "records");

        List<CardExceptionRecordVO> voList = entityPage.getRecords().stream().map(entity -> {
            CardExceptionRecordVO cardExceptionRecordVO = new CardExceptionRecordVO();
            BeanUtils.copyProperties(entity, cardExceptionRecordVO);
            return cardExceptionRecordVO;
        }).collect(Collectors.toList());
        voPage.setRecords(voList);

        return voPage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteCardExceptionRecord(Long id) {
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteCardExceptionRecord(List<Long> ids) {
        return this.removeByIds(ids);
    }
}